package com.ly.leave.controller;

import java.util.List;


import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.page.TableDataInfo;
import com.ly.common.enums.BusinessType;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.leave.domain.WorkflowLeave;
import com.ly.leave.service.IWorkflowLeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 请假Controller
 * @author danny
 */
@Api(value = "WorkflowLeave", tags = "请假")
@RestController
@RequestMapping ("/workflow/leave")
public class WorkflowLeaveController extends BaseController {

    @Resource
    private IWorkflowLeaveService workflowLeaveService;

    /**
     * 查询请假列表
     */
    @PreAuthorize ("@ss.hasPermi('workflow:leave:list')")
    @GetMapping ("/list")
    public TableDataInfo list (WorkflowLeave workflowLeave) {
        startPage ();
        workflowLeave.setCreateBy ( SecurityUtils.getUsername () );
        List<WorkflowLeave> list = workflowLeaveService.selectWorkflowLeaveAndTaskNameList ( workflowLeave );
        return getDataTable ( list );
    }

    /**
     * 查询请假列表
     */
    @PreAuthorize ("@ss.hasPermi('workflow:leave:list')")
    @GetMapping ("/listAll")
    public TableDataInfo listAll (WorkflowLeave workflowLeave) {
        startPage ();
        List<WorkflowLeave> list = workflowLeaveService.selectWorkflowLeaveList ( workflowLeave );
        return getDataTable ( list );
    }

    /**
     * 导出请假列表
     */
    @PreAuthorize ("@ss.hasPermi('workflow:leave:export')")
    @Log (title = "请假", businessType = BusinessType.EXPORT)
    @GetMapping ("/export")
    public AjaxResult export (WorkflowLeave workflowLeave) {
        List<WorkflowLeave> list = workflowLeaveService.selectWorkflowLeaveList ( workflowLeave );
        ExcelUtil<WorkflowLeave> util = new ExcelUtil<WorkflowLeave> ( WorkflowLeave.class );
        return util.exportExcel ( list, "leave" );
    }

    /**
     * 获取请假详细信息
     */
    @PreAuthorize ("@ss.hasPermi('workflow:leave:query')")
    @GetMapping (value = "/{id}")
    public AjaxResult getInfo (@PathVariable ("id") String id) {
        return AjaxResult.success ( workflowLeaveService.selectWorkflowLeaveById ( id ) );
    }

    /**
     * 获取请假详细信息
     */
    @PreAuthorize ("@ss.hasPermi('workflow:leave:query')")
    @GetMapping (value = "ByInstanceId/{instanceId}")
    public AjaxResult getInfoByInstanceId (@PathVariable ("instanceId") String instanceId) {
        return AjaxResult.success ( workflowLeaveService.selectWorkflowLeaveByInstanceId ( instanceId ) );
    }

    /**
     * 新增请假
     */
//    @ApiOperation( "新增请假申请" )
////    @PreAuthorize ("@ss.hasPermi('workflow:leave:add')")
//    @Log (title = "请假", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add (@RequestBody WorkflowLeave workflowLeave) {
//        return toAjax ( workflowLeaveService.insertWorkflowLeave ( workflowLeave ) );
//    }

    /**
     * 修改请假
     */
//    @PreAuthorize ("@ss.hasPermi('workflow:leave:edit')")
//    @Log (title = "请假", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit (@RequestBody WorkflowLeave workflowLeave) {
//        return toAjax ( workflowLeaveService.insertWorkflowLeave ( workflowLeave ) );
//    }

    /**
     * 删除请假
     */
    @PreAuthorize ("@ss.hasPermi('workflow:leave:remove')")
    @Log (title = "请假", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{ids}")
    public AjaxResult remove (@PathVariable String[] ids) {
        return toAjax ( workflowLeaveService.deleteWorkflowLeaveByIds ( ids ) );
    }
}
